package Classes;

import Classes.PersonExtends.Hero;
import Classes.PersonExtends.Person;
import Interface.IObjectView;
import Utils.EnumControllerButton;
import Utils.EnumDirect;
import Utils.GameConfig;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SceneView extends SurfaceView implements SurfaceHolder.Callback,IObjectView {

	DrawThread dt;
	
	SceneBuilder sb;
	
	int screenHeight=320;
	int screenWidth=480;
	
	
	float centerCorrectionX;
	float centerCorrectionY;
	
	float centerX;
	float centerY;
	
	
	GameController gameController;
	EventManager eventManager;
	MenuManager menuManager;
	public SceneView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		getHolder().addCallback(this);
		Resources r=getResources();
		
		sb=new SceneBuilder(r);
		dt=new DrawThread(this,getHolder(),0); 

//	    screenHeight=getHeight();
//	    screenWidth=getWidth();
		
		centerX=screenWidth/2;
		centerY=screenHeight/2;
		centerCorrectionX=centerX-GameConfig.BITSIZE/2;
		centerCorrectionY=centerY-GameConfig.BITSIZE/2;
	    
		gameController=GameController.getInstance();
		eventManager=EventManager.getInstance();
		menuManager= MenuManager.getInstance();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub

		if(!dt.isAlive()){
			dt.start();
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub

		dt.flag=false;
		dt= null;
	}

	@Override
	public void doDraw(Canvas canvas) {
		// TODO Auto-generated method stub

		canvas.drawColor(Color.BLACK);
		Scene scene=sb.currentScene;
		Hero hero=scene.hero;
		
		canvas.drawBitmap(scene.map.btMapSrc, centerCorrectionX-hero.x*GameConfig.BITSIZE-hero.detaX*hero.detaMove ,  centerCorrectionY-hero.y*GameConfig.BITSIZE-hero.detaY*hero.detaMove ,null);

		int npcsize=scene.alPerson.size();
		
		for(int i=0;i<npcsize;i++){
			Person p=scene.alPerson.get(i);
		 p.drawDirectImage(canvas,  centerCorrectionX-(hero.x-p.x)*GameConfig.BITSIZE-hero.detaX*hero.detaMove, centerCorrectionY-(hero.y-p.y)*GameConfig.BITSIZE-hero.detaY*hero.detaMove,null);
		}
		
		hero.drawDirectImage(canvas, centerCorrectionX, centerCorrectionY,null);
		
		
		
		eventManager.drawDialog(canvas);
		
		  menuManager.draw(canvas);

			gameController.setController(canvas);
	}

	public void doControllerInteraction(EnumControllerButton irButton) {
		// TODO Auto-generated method stub
		if(sb.currentScene.hero.isDoSomething==false){
			int detaX=0;
			int detaY=0;
			if(irButton==EnumControllerButton.UP){
				detaY=-1;
				sb.currentScene.setHeroDirect(EnumDirect.NORTH);
			}
			else if(irButton==EnumControllerButton.DOWN){
					detaY=1;
					sb.currentScene.setHeroDirect(EnumDirect.SOUTH);
			}
			else if(irButton==EnumControllerButton.LEFT){
				detaX=-1;
				sb.currentScene.setHeroDirect(EnumDirect.WEST);
			}
			else if(irButton==EnumControllerButton.RIGHT){
				detaX=1;
				sb.currentScene.setHeroDirect(EnumDirect.EAST);
			}
			else if(irButton==EnumControllerButton.A){
				sb.currentScene.heroToDo();
			}
			else if(irButton==EnumControllerButton.B){
				gameController.callMenu();
			}
			
			if(detaX!=0||detaY!=0){
				boolean canGo=sb.currentScene.checkCanGo(detaX,detaY);
				if(canGo){
					sb.currentScene.heroGo(detaX,detaY);
				}
			}
		}
	}



}
